var VisitorTable = function () {
  var SearchForm = function () {
    var template = /* html */ `
      <div class="search-form">
        <el-form inline label-width="auto">
          <el-form-item label="姓名">
            <el-input v-model="search.userName" class="search-input"></el-input>
          </el-form-item>
        </el-form>
      </div>
    `;

    return {
      props: {
        search: {
          type: Object,
          required: true
        }
      },

      data: function () {
        return {};
      },

      template: template
    };
  }();

  var template = /* html */ `
    <div class="visitor-table" v-loading.fullscreen.lock="loading">
      <search-btn-view append-to-body btn-text="查询" @before-open="resetSearch" @search="searchData">
        <div slot="search-content">
          <search-form :search="search"></search-form>
        </div>
      </search-btn-view>

      <div class="visitor-table-table-div">
        <el-table fit stripe border :data="visitorList" style="width: 100%" height="parent">
          <el-table-column label="姓名" prop="userName" min-width="120px" header-align="center"></el-table-column>
          <el-table-column label="身份证号" prop="idCard" min-width="160px" header-align="center"></el-table-column>
          <el-table-column label="手机号" prop="phone" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="头像" width="90px" align="center">
            <template slot-scope="scope">
              <el-image class="visitor-img" :src="formatImgUrl(scope.row.personImg)" fit="scale-down" :preview-src-list="[scope.row.personImg]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" width="80px" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="delVisitor(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div style="margin-top:10px;text-align:right;" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
          @current-change="pageChange"></el-pagination>
      </div>
    </div>
  `;

  return {
    data: function () {
      var search = {
        userName: ''
      };
      return {
        loading: false,
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        visitorList: [],
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        }
      };
    },

    created: function () {
      this.setVisitorList(true);
    },

    methods: {
      setVisitorList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize,
        };

        var search = this.search;
        var userName = search.userName;
        if (userName !== '') {
          postData.userName = userName;
        }

        this.loading = true;
        var self = this;
        ajaxRequest('visitor/queryVisitorList', postData).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }
          var visitorList = res.returnData;
          if (visitorList.length === 0 && currentPage !== 1) {
            self.pagination.totalCount = res.iTotalRecords;
            self.$nextTick(function(){
              self.setVisitorList(isSearch);
            });
            return;
          }

          for (var i = 0; i < visitorList.length; i++) {
            var visitor = visitorList[i];
            var personImg = visitor.personImg;
            visitor.personImg = personImg;
          }

          self.visitorList = visitorList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false
        });
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.setVisitorList(true);
      },

      pageChange: function () {
        this.setVisitorList();
      },

      delVisitor: function (visitor) {
        var self = this;
        this.$confirm('是否确定删除该访客信息', '删除确认', {
          confirmButtonText: '是',
          cancelButtonText: '否'
        }).then(function () {
          self.toDelVisitor(visitor);
        }).catch(function () {});
      },

      toDelVisitor: function (visitor) {
        this.loading = true;
        var self = this;
        ajaxRequest('visitor/deleteVisitorById', {
          userId: visitor.id
        }).then(function () {
          self.loading = false;
          self.$alert('访客信息成功删除','信息',{
            callback:function(){
              self.setVisitorList();
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      SearchForm: SearchForm
    },

    template: template
  };
}();
